/**
 * @(#)ProcessHeader.java Nov 27, 2009
 *   
 * Copyright  (c) 2009-2010 Wilmar IT Service,  Inc.   
 * All  rights  reserved.   
 * TODO
 */

package com.wilmar.process.web.taglibs;

import java.util.Locale;

import javax.servlet.jsp.JspWriter;

import com.wilmar.process.core.ProcessSystemDeclareKeys;

/**
 * @author MaKuBEX Email:makubexnet@gmail.com
 * @version 1.0.0
 */
public class ProcessHeaderTag extends ProcessTagSupport {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = 7092085365883856067L;

	private String scriptbase;
	private String stylebase;
	private String ckbase;

	private String language;

	private boolean jquerybase = true;
	private boolean autocomplete = false;
	private boolean message = false;
	private boolean tooltip = false;
	private boolean datatable = false;
	private boolean form = false;
	private boolean pagestatic = false;
	private boolean ui = false;
	private boolean autoui = true;
	private boolean ckeditor = false;

	/**
	 * @see org.springframework.web.servlet.tags.RequestContextAwareTag#doStartTagInternal()
	 */
	protected int doStartTagInternal() throws Exception {
		String c_scriptbase = null;
		String c_stylebase = null;
		if (this.scriptbase != null && this.scriptbase.trim().length() != 0) {
			c_scriptbase = super.getContextPath()
					+ ProcessSystemDeclareKeys.PAGE_BASE_DIR
					+ ProcessSystemDeclareKeys.SLASH + this.scriptbase;
		} else {
			c_scriptbase = super.getContextPath()
					+ ProcessSystemDeclareKeys.PAGE_SCRIPT_DIR;
		}
		if (this.stylebase != null && this.stylebase.trim().length() != 0) {
			c_stylebase = super.getContextPath()
					+ ProcessSystemDeclareKeys.PAGE_BASE_DIR
					+ ProcessSystemDeclareKeys.SLASH + this.stylebase;
		} else {
			c_stylebase = super.getContextPath()
					+ ProcessSystemDeclareKeys.PAGE_STYLE_DIR;
		}
		StringBuffer html = new StringBuffer();
		html.append("<script type=\"text/javascript\" src=\"");
		html.append(c_scriptbase);
		html.append(ProcessSystemDeclareKeys.SLASH
				+ "process_system_declare.js\"></script>\n");
		Locale locale = null;
		if (this.language != null && this.language.trim().length() > 0) {
			try {
				locale = new Locale(this.language.trim());
			} catch (Exception e) {
				locale = super.getUserLocale();
			}
		} else {
			locale = super.getUserLocale();
		}
		html.append("<script type=\"text/javascript\" src=\"");
		html.append(c_scriptbase);
		html.append(ProcessSystemDeclareKeys.SLASH + "process_system_message_"
				+ locale.toString() + ".js\"></script>\n");
		html.append("<script type=\"text/javascript\">\n");
		html
				.append("SYSTEM_CONTEXT_PATH = '" + super.getContextPath()
						+ "';\n");
		html.append("SYSTEM_CONTEXT_HOST = '"
				+ super.pageContext.getRequest().getLocalAddr() + "';\n");
		html.append("SYSTEM_CONTEXT_PROT = '"
				+ super.pageContext.getRequest().getLocalPort() + "';\n");
		html.append("</script>\n");
		if (this.jquerybase) {
			html.append("<script type=\"text/javascript\" src=\"");
			html.append(c_scriptbase);
			html.append(ProcessSystemDeclareKeys.SLASH + "jquery"
					+ ProcessSystemDeclareKeys.SLASH
					+ "jquery.js\"></script>\n");
			html.append("<script type=\"text/javascript\" src=\"");
			html.append(c_scriptbase);
			html.append(ProcessSystemDeclareKeys.SLASH + "jquery"
					+ ProcessSystemDeclareKeys.SLASH
					+ "jquery.json.js\"></script>\n");
		}
		if (this.ckbase != null && this.ckbase.trim().length() > 0) {
			if (!this.ckbase.trim().endsWith("/"))
				this.ckbase = this.ckbase.trim()
						+ ProcessSystemDeclareKeys.SLASH;
			html.append("<script type=\"text/javascript\" src=\"");
			html.append(super.getContextPath());
			html.append(ProcessSystemDeclareKeys.SLASH + this.ckbase
					+ "ckeditor.js\"></script>\n");
			html.append("<script type=\"text/javascript\" src=\"");
			html.append(super.getContextPath());
			html.append(ProcessSystemDeclareKeys.SLASH + this.ckbase
					+ ProcessSystemDeclareKeys.SLASH + "adapters"
					+ ProcessSystemDeclareKeys.SLASH
					+ "jquery.js\"></script>\n");
		} else if (this.ckeditor && this.ckbase == null) {
			html.append("<script type=\"text/javascript\" src=\"");
			html.append(c_scriptbase);
			html.append(ProcessSystemDeclareKeys.SLASH + "ckeditor"
					+ ProcessSystemDeclareKeys.SLASH
					+ "ckeditor.js\"></script>\n");
			html.append("<script type=\"text/javascript\" src=\"");
			html.append(c_scriptbase);
			html.append(ProcessSystemDeclareKeys.SLASH + "ckeditor"
					+ ProcessSystemDeclareKeys.SLASH + "adapters"
					+ ProcessSystemDeclareKeys.SLASH
					+ "jquery.js\"></script>\n");
		}
		if (this.pagestatic) {
			html.append("<script type=\"text/javascript\" src=\"");
			html.append(c_scriptbase);
			html.append(ProcessSystemDeclareKeys.SLASH
					+ "process_page_static.js\"></script>\n");
		}
		if (this.datatable) {
			html.append("<script type=\"text/javascript\" src=\"");
			html.append(c_scriptbase);
			html.append(ProcessSystemDeclareKeys.SLASH + "jquery"
					+ ProcessSystemDeclareKeys.SLASH + "dataTable"
					+ ProcessSystemDeclareKeys.SLASH
					+ "jquery.dataTables.js\"></script>\n");
			html.append("<link rel=\"stylesheet\" href=\"");
			html.append(c_stylebase);
			html.append(ProcessSystemDeclareKeys.SLASH + "jquery"
					+ ProcessSystemDeclareKeys.SLASH + "dataTable"
					+ ProcessSystemDeclareKeys.SLASH
					+ "demo_page.css\" type=\"text/css\" media=\"screen\"/>\n");
			html.append("<link rel=\"stylesheet\" href=\"");
			html.append(c_stylebase);
			html
					.append(ProcessSystemDeclareKeys.SLASH
							+ "jquery"
							+ ProcessSystemDeclareKeys.SLASH
							+ "dataTable"
							+ ProcessSystemDeclareKeys.SLASH
							+ "demo_table.css\" type=\"text/css\" media=\"screen\"/>\n");
		}
		if (this.message) {
			html.append("<script type=\"text/javascript\" src=\"");
			html.append(c_scriptbase);
			html.append(ProcessSystemDeclareKeys.SLASH + "jquery"
					+ ProcessSystemDeclareKeys.SLASH + "blockUI"
					+ ProcessSystemDeclareKeys.SLASH
					+ "jquery.blockUI.js\" ></script>\n");
		}
		if (this.ui) {
			html.append("<link rel=\"stylesheet\" href=\"");
			html.append(c_stylebase);
			html
					.append(ProcessSystemDeclareKeys.SLASH
							+ "jquery"
							+ ProcessSystemDeclareKeys.SLASH
							+ "ui"
							+ ProcessSystemDeclareKeys.SLASH
							+ "ui-lightness"
							+ ProcessSystemDeclareKeys.SLASH
							+ "jquery-ui-1.8.1.custom.css\" type=\"text/css\" media=\"screen\"/>\n");
			html.append("<script type=\"text/javascript\" src=\"");
			html.append(c_scriptbase);
			html.append(ProcessSystemDeclareKeys.SLASH + "jquery"
					+ ProcessSystemDeclareKeys.SLASH + "ui"
					+ ProcessSystemDeclareKeys.SLASH
					+ "jquery-ui-1.8.1.custom.min.js\"></script>\n");
			if (this.autoui) {
				html.append("<script type=\"text/javascript\" src=\"");
				html.append(c_scriptbase);
				html.append(ProcessSystemDeclareKeys.SLASH
						+ "process_system_applyui.js\"></script>\n");
			}
		}
		if (this.tooltip) {
			html.append("<script type=\"text/javascript\" src=\"");
			html.append(c_scriptbase);
			html.append(ProcessSystemDeclareKeys.SLASH + "jquery"
					+ ProcessSystemDeclareKeys.SLASH + "tools"
					+ ProcessSystemDeclareKeys.SLASH + "tooltip"
					+ ProcessSystemDeclareKeys.SLASH
					+ "jquery.tools.min.js\"></script>\n");
			html.append("<link rel=\"stylesheet\" href=\"");
			html.append(c_stylebase);
			html
					.append(ProcessSystemDeclareKeys.SLASH
							+ "jquery"
							+ ProcessSystemDeclareKeys.SLASH
							+ "tools"
							+ ProcessSystemDeclareKeys.SLASH
							+ "tooltip"
							+ ProcessSystemDeclareKeys.SLASH
							+ "common_tooltip.css\" type=\"text/css\" media=\"screen\"/>\n");
		}
		if (this.autocomplete) {
			html.append("<link type=\"text/css\" href=\"");
			html.append(c_stylebase);
			html.append(ProcessSystemDeclareKeys.SLASH + "jquery"
					+ ProcessSystemDeclareKeys.SLASH + "autocomplete"
					+ ProcessSystemDeclareKeys.SLASH
					+ "autocomplete.css\" rel=\"stylesheet\">\n");
			html.append("<script type=\"text/javascript\" src=\"");
			html.append(c_scriptbase);
			html.append(ProcessSystemDeclareKeys.SLASH + "jquery"
					+ ProcessSystemDeclareKeys.SLASH + "autocomplete"
					+ ProcessSystemDeclareKeys.SLASH
					+ "jquery.autocomplete.js\"></script>\n");
		}
		if (this.form) {
			html.append("<script type=\"text/javascript\" src=\"");
			html.append(c_scriptbase);
			html.append(ProcessSystemDeclareKeys.SLASH
					+ "process_system_form.js\"></script>\n");
		}
		JspWriter outer = this.getOut();
		outer.write(html.toString());
		return SKIP_BODY;
	}

	/**
	 * @return the scriptbase
	 */
	public String getScriptbase() {
		return scriptbase;
	}

	/**
	 * @param scriptbase
	 *            the scriptbase to set
	 */
	public void setScriptbase(String scriptbase) {
		this.scriptbase = scriptbase;
	}

	/**
	 * @return the stylebase
	 */
	public String getStylebase() {
		return stylebase;
	}

	/**
	 * @param stylebase
	 *            the stylebase to set
	 */
	public void setStylebase(String stylebase) {
		this.stylebase = stylebase;
	}

	/**
	 * @return the jquerybase
	 */
	public boolean isJquerybase() {
		return jquerybase;
	}

	/**
	 * @param jquerybase
	 *            the jquerybase to set
	 */
	public void setJquerybase(boolean jquerybase) {
		this.jquerybase = jquerybase;
	}

	/**
	 * @return the autocomplete
	 */
	public boolean isAutocomplete() {
		return autocomplete;
	}

	/**
	 * @param autocomplete
	 *            the autocomplete to set
	 */
	public void setAutocomplete(boolean autocomplete) {
		this.autocomplete = autocomplete;
	}

	/**
	 * @return the tooltip
	 */
	public boolean isTooltip() {
		return tooltip;
	}

	/**
	 * @param tooltip
	 *            the tooltip to set
	 */
	public void setTooltip(boolean tooltip) {
		this.tooltip = tooltip;
	}

	/**
	 * @return the datatable
	 */
	public boolean isDatatable() {
		return datatable;
	}

	/**
	 * @param datatable
	 *            the datatable to set
	 */
	public void setDatatable(boolean datatable) {
		this.datatable = datatable;
	}

	/**
	 * @return the form
	 */
	public boolean isForm() {
		return form;
	}

	/**
	 * @param form
	 *            the form to set
	 */
	public void setForm(boolean form) {
		this.form = form;
	}

	/**
	 * @param pagestatic
	 *            the pagestatic to set
	 */
	public void setPagestatic(boolean pagestatic) {
		this.pagestatic = pagestatic;
	}

	/**
	 * @return the pagestatic
	 */
	public boolean isPagestatic() {
		return pagestatic;
	}

	/**
	 * @return the message
	 */
	public boolean isMessage() {
		return message;
	}

	/**
	 * @param message
	 *            the message to set
	 */
	public void setMessage(boolean message) {
		this.message = message;
	}

	/**
	 * @return the language
	 */
	public String getLanguage() {
		return language;
	}

	/**
	 * @param language
	 *            the language to set
	 */
	public void setLanguage(String language) {
		this.language = language;
	}

	/**
	 * @return the ui
	 */
	public boolean isUi() {
		return ui;
	}

	/**
	 * @param ui
	 *            the ui to set
	 */
	public void setUi(boolean ui) {
		this.ui = ui;
	}

	/**
	 * @return the autoui
	 */
	public boolean isAutoui() {
		return autoui;
	}

	/**
	 * @param autoui
	 *            the autoui to set
	 */
	public void setAutoui(boolean autoui) {
		this.autoui = autoui;
	}

	/**
	 * @return the ckbase
	 */
	public String getCkbase() {
		return ckbase;
	}

	/**
	 * @param ckbase
	 *            the ckbase to set
	 */
	public void setCkbase(String ckbase) {
		this.ckbase = ckbase;
	}

	/**
	 * @return the ckeditor
	 */
	public boolean isCkeditor() {
		return ckeditor;
	}

	/**
	 * @param ckeditor
	 *            the ckeditor to set
	 */
	public void setCkeditor(boolean ckeditor) {
		this.ckeditor = ckeditor;
	}

}
